package com.kws.commercial.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kws.commercial.dao.CommercialProjectScheduleManagement;
import com.kws.commercial.dao.Info.QueryProjectFileInfo;
import com.kws.commercial.dao.Info.QueryProjectScheduleInfo;
import com.kws.commercial.dao.Rq.CreateProjectScheduleRq;
import com.kws.commercial.dao.Rq.UploadProjectScheduleFileRq;
import com.kws.commercial.mapper.CommercialProjectScheduleMapper;
import com.kws.commercial.service.CommercialProjectDeviceService;
import com.kws.commercial.service.CommercialProjectScheduleService;
import com.kws.commercial.service.CommercialScheduleFileService;
import com.kws.common.core.enums.ModelOpType;
import com.kws.common.core.sys.Ksys;
import com.kws.common.core.utils.KConstants;
import com.kws.common.security.utils.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <b>CommercialProjectScheduleServiceImpl</b>
 *
 * @description: CommercialProjectScheduleServiceImpl <br>
 * @date: 2024/10/29 15:27 <br>
 * @author: lhm <br>
 * @version: 1.0 <br>
 */

@Service
public class CommercialProjectScheduleServiceImpl extends ServiceImpl<CommercialProjectScheduleMapper, CommercialProjectScheduleManagement> implements CommercialProjectScheduleService {
    private static final Logger logger = LoggerFactory.getLogger(CommercialProjectScheduleServiceImpl.class);

    @Autowired
    private CommercialScheduleFileService commercialScheduleFileService;

    @Autowired
    private CommercialProjectDeviceService commercialProjectDeviceService;


    @Override
    public PageInfo<QueryProjectScheduleInfo> queryProjectSchedulePage(Integer pageNum, Integer pageSize, String schemeName) {
        PageHelper.startPage(pageNum,pageSize);
        Long companyId = SecurityUtils.getCompanyId();
        List<QueryProjectScheduleInfo> resultList = baseMapper.queryProjectSchedulePage(schemeName,companyId);
        return new PageInfo<QueryProjectScheduleInfo>(resultList);
    }

    @Override
    @Transactional
    public boolean createProjectSchedule(CreateProjectScheduleRq rq) {
        CommercialProjectScheduleManagement commercialProjectScheduleManagement = new CommercialProjectScheduleManagement();
        commercialProjectScheduleManagement.setCompanyId(SecurityUtils.getCompanyId());
        commercialProjectScheduleManagement.setProjectQuotationId(rq.getProjectQuotationId());
        commercialProjectScheduleManagement.setProjectStartTime(rq.getProjectStartTime());
        commercialProjectScheduleManagement.setProjectEndTime(rq.getProjectEndTime());
        commercialProjectScheduleManagement.setStatus("0");
        Ksys.setBasicModelData(commercialProjectScheduleManagement,SecurityUtils.getStringUserId(), KConstants.DATA_ENABLE, ModelOpType.CREATE);
        int insert = baseMapper.insert(commercialProjectScheduleManagement);
        commercialProjectDeviceService.createProjectDevice(rq);
        if (1 == insert){
            return true;
        }
        return false;
    }

    @Override
    public QueryProjectScheduleInfo queryProjectScheduleById(Long id) {
        QueryProjectScheduleInfo info = baseMapper.queryProjectScheduleById(id);
        if (null != info) {
            List<QueryProjectFileInfo> fileList = commercialScheduleFileService.queryProjectScheduleById(info.getProjectQuotationId());
            if (CollectionUtil.isNotEmpty(fileList)){
                Map<String, List<QueryProjectFileInfo>> fileMap = fileList.stream()
                        .collect(Collectors.groupingBy(
                                QueryProjectFileInfo::getProjectStatus,
                                Collectors.collectingAndThen(
                                        Collectors.toList(),
                                        list -> {
                                            list.sort(Comparator.comparing(QueryProjectFileInfo::getProjectStatus));
                                            return list;
                                        }
                                )
                        ));
                info.setFileMap(fileMap);
            }
        }
        return info;
    }

    @Override
    public Map<String, List<QueryProjectFileInfo>> queryProjectScheduleInfoById(Long id) {
        QueryProjectScheduleInfo info = baseMapper.queryProjectScheduleById(id);
        if (null != info) {
            List<QueryProjectFileInfo> fileList = commercialScheduleFileService.queryProjectScheduleById(info.getProjectQuotationId());
            if (CollectionUtil.isNotEmpty(fileList)){
                Map<String, List<QueryProjectFileInfo>> fileMap = fileList.stream()
                        .collect(Collectors.groupingBy(
                                QueryProjectFileInfo::getProjectStatus,
                                Collectors.collectingAndThen(
                                        Collectors.toList(),
                                        list -> {
                                            list.sort(Comparator.comparing(QueryProjectFileInfo::getProjectStatus));
                                            return list;
                                        }
                                )
                        ));
                return fileMap;
            }
        }
        return null;
    }

    @Override
    public Map<String, List<QueryProjectFileInfo>> uploadProjectScheduleInfoByProjectQuotationId(UploadProjectScheduleFileRq rq) {
        List<QueryProjectFileInfo> fileList = commercialScheduleFileService.uploadProjectScheduleInfoByProjectQuotationId(rq);
        Map<String, List<QueryProjectFileInfo>> fileMap = fileList.stream()
                .collect(Collectors.groupingBy(
                        QueryProjectFileInfo::getProjectStatus,
                        Collectors.collectingAndThen(
                                Collectors.toList(),
                                list -> {
                                    list.sort(Comparator.comparing(QueryProjectFileInfo::getProjectStatus));
                                    return list;
                                }
                        )
                ));
        return fileMap;
    }
}
